package com.wn.consumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.wn.client.UserDubboClient;
import com.wn.domain.User;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yellow Docter
 * @date 2022 -10-31
 * @desc
 */
@RestController
@RequestMapping("user")
public class UserController {

    // dubbo 远程调用
    @DubboReference(check = false)
    private UserDubboClient userDubboClient;

    @HystrixCommand(fallbackMethod = "errorFallback")
    @GetMapping("/{id}")
    public User findUserById(@PathVariable("id")Integer id) throws Exception {

        if (id > 10){
            throw new Exception("出异常了...");
        }

        User user = userDubboClient.findUserById(id);
        return user;
    }

    @DeleteMapping("/{id}")
    public String removeUserById(@PathVariable("id")Integer id) throws Exception {


        // dubbo 的远程调用
        String s = userDubboClient.removeUserById(id);
        return s;
    }

    @GetMapping("list")
    public List<User> findAll(){
        List<User> list = userDubboClient.findAll();
        return list;
    }
    public User errorFallback(Integer id){
        User user = new User();
        user.setName("默认的数据");
        user.setAge(120);
        return user;
    }


}
